/*
 * RequestForm
 * 
 * Version information
 * 
 * Mar 24, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.icontact.webapp.struts.form;

import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import net.amsoft.iservice.icontact.util.IContactConstants;
import net.amsoft.iservice.util.IServiceUtil;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 * @author rahul
 * 
 */
@SuppressWarnings("serial")
public class RequestForm extends ActionForm {
    private static final Logger oLogger = Logger.getLogger(RequestForm.class);

    private String sSenderIname = "";

    private String sSenderName = "";

    private String sSenderEmail = "";

    private String sMsg = "";

    private String sSynonym = "";

    private boolean bRequest = true;

    private String sHash = "";

    private String sVerifyingPattern = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\."
            + "[A-Za-z0-9-]+)*(\\.[_A-Za-z0-9-]+)";

    private Pattern p = Pattern
            .compile(sVerifyingPattern, Pattern.CASE_INSENSITIVE
                    | Pattern.UNICODE_CASE | Pattern.MULTILINE);

    public String getMsg() {
        return sMsg;
    }

    public void setMsg(String msg) {
        // XSS protection: replaces < and > with HTML quivalents codes
        this.sMsg = msg.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    }

    public String getSenderEmail() {
        return sSenderEmail;
    }

    public void setSenderEmail(String senderEmail) {
        this.sSenderEmail = senderEmail;
    }

    public String getSenderIname() {
        return sSenderIname;
    }

    public void setSenderIname(String senderIname) {
        this.sSenderIname = senderIname;
    }

    public String getSenderName() {
        return sSenderName;
    }

    public void setSenderName(String senderName) {
        this.sSenderName = senderName;
    }

    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {

        ResourceBundle oBundle = ResourceBundle.getBundle("icontactResources",
                request.getLocale());

        if (!"save".equals(request.getParameter("step"))) {
            return null;
        }
        ActionErrors oErrs = new ActionErrors();
        if (sSenderIname != null && sSenderIname.length() > 0) {
            sSenderIname = sSenderIname.toLowerCase().trim();
            if (sSenderIname.length() > 255) {
                sSenderIname = sSenderIname.substring(0, 255);
            }

            // Get i-name from XRI/HXRI
            sSenderIname = IServiceUtil.getIName(sSenderIname);
            if (sSenderIname != null) {
                sSenderIname = sSenderIname.toLowerCase().trim();
            } else {
            	oErrs.add("senderIname", new ActionMessage(IContactConstants.ERR_INVALID,
            	                    "I-Name"));
            }
            
        }
        if (sSenderEmail != null && sSenderEmail.length() > 0) {
            Matcher m = p.matcher(sSenderEmail);
            if (m.matches() == false) {
                oErrs.add("senderEmail", new ActionMessage(
                        IContactConstants.ERR_INVALID, oBundle
                                .getString(IContactConstants.EMAIL_ADDRESS)));
            }
        }
        //if (sMsg == null || sMsg.length() == 0) {
        //    oErrs.add("msg", new ActionMessage(IContactConstants.ERR_REQUIRED,
        //            oBundle.getString(IContactConstants.MESSAGE)));
        //} else {
            sMsg = sMsg.trim();
            if (sMsg.length() > 1000) {
                sMsg = sMsg.substring(0, 1000);
            }
        //}
        if (sSenderName == null || sSenderName.length() == 0) {
            sSenderName = oBundle
                    .getString(IContactConstants.DEFAULT_SENDER_NAME);
        } else {
            sSenderName = sSenderName.trim();
            if (sSenderName.length() > 255) {
                sSenderName = sSenderName.substring(0, 255);
            }
        }
        return oErrs;
    }

    public String getSynonym() {
        return sSynonym;
    }

    public void setSynonym(String synonym) {
        sSynonym = synonym;
    }

    public boolean isRequest() {
        return bRequest;
    }

    public void setRequest(boolean isRequest) {
        bRequest = isRequest;
    }

    public String getHash() {
        return sHash;
    }

    public void setHash(String hash) {
        sHash = hash;
    }

    public void debug() {
        oLogger.debug("RequestForm: SINAME=" + sSenderIname + ", SNAME="
                + sSenderName + ", SEMAIL=" + sSenderEmail + ", MSG=" + sMsg
                + ", SYNONYM=" + sSynonym);
    }
}
